// CadToolRectangle.cpp
//
#include "CadToolRectangle.h"

#include "CadEngine.h"

#include "OpenGL/Box.h"
#include "OpenGL/OpenGL.h"
#include "OpenGL/Renderer.h"

//-------------------------------------------------------------------------------------------------
CadToolRectangle::CadToolRectangle()
: CadTool("Rectangle",2)
{
	_creationColor = OpenGL::Color::blue();
	_square = false;
}

//-------------------------------------------------------------------------------------------------
CadToolRectangle::~CadToolRectangle()
{}

//-------------------------------------------------------------------------------------------------
void CadToolRectangle::execute()
{
	if( OpenGL::Math::distance(_points[0],_points[1]) < _manager->pixelSize() )
		return;

	CadEngine::Command c;

	c.command   = CadEngine::Create;
	c.primitive = CadEngine::Quad;
	c.color     = _creationColor;
	if( _square )
	{
		const double diag = OpenGL::Math::distance(_points[0],_points[1]);
		const double side = diag/OpenGL::Math::sqrt(2.0);

		OpenGL::Math::dvec3 p;
		p.x = _points[0].x + side * OpenGL::Math::sign( _points[1].x - _points[0].x );
		p.y = _points[0].y + side * OpenGL::Math::sign( _points[1].y - _points[0].y );

		c.points.push_back( _points[0] );
		c.points.push_back( p );
		c.attributes += _objectAttributes;
	}
	else
	{
		c.points.push_back( _points[0] );
		c.points.push_back( _points[1] );
		c.attributes += _objectAttributes;
	}

	_manager->issueCommand(c);
}

// --------------------------------------------------------------------------------------------
void CadToolRectangle::setCreationColor( const OpenGL::Color &c ) { _creationColor = c; }

// --------------------------------------------------------------------------------------------
OpenGL::Color CadToolRectangle::creationColor() const { return _creationColor; }

//-------------------------------------------------------------------------------------------------
void CadToolRectangle::draw( OpenGL::Renderer *r )
{
	if(_nPoints < 2) return;

	OpenGL::Math::dvec3 o = r->origin();

	OpenGL::Math::dvec3 p = _points[1];
	if( _square )
	{
		const double diag = OpenGL::Math::distance(_points[0],_points[1]);
		const double side = diag/OpenGL::Math::sqrt(2.0);

		p.x = _points[0].x + side * OpenGL::Math::sign( _points[1].x - _points[0].x );
		p.y = _points[0].y + side * OpenGL::Math::sign( _points[1].y - _points[0].y );
	}


	OpenGL::Math::box3 box;
	box.extend( OpenGL::Math::dvec3(_points[0].x,_points[0].y,0)-o );
	box.extend( OpenGL::Math::dvec3(p.x,p.y,0)-o );

	glPushAttrib( GL_LINE_BIT|GL_ENABLE_BIT );
	glEnable(GL_LINE_STIPPLE);
	glLineStipple(1, 0x00FF);
	glLineWidth(2);	
	glColor4fv( _manager->settings().previewColor().rgba() );

	// borders
	glBegin( GL_LINE_LOOP);
		glVertex2d( box.minPoint().x, box.maxPoint().y );
		glVertex2d( box.minPoint().x, box.minPoint().y );
		glVertex2d( box.maxPoint().x, box.minPoint().y );
		glVertex2d( box.maxPoint().x, box.maxPoint().y );
	glEnd();

	glPopAttrib();
}